package com.platform.cdcs.fragment.custom;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.fragment.choose.AccountChooseFragment;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.MockObj;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.model.RegCodeItem;
import com.platform.cdcs.model.SubBUItem;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.platform.cdcs.tool.ViewTool;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.model.ChooseItem;
import com.trueway.app.uilib.tool.Utils;
import com.trueway.app.uilib.widget.TimeDialogBuilder;
import com.trueway.app.uilib.widget.TwDialogBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Call;

/**
 * Created by holytang on 2017/9/28.
 */
public class AddRegNumberFragment extends BaseFragment {

    private EditText codeET, nameET, regCodeET, batchCodeET, startET, endET, bakEt;
    private RegCodeItem item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            item = (RegCodeItem) getArguments().getSerializable("model");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initView(View view) {
        if (item == null) {
            setTitle("新增注册证号");
        } else {
            setTitle("修改注册证号");
        }
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout root1 = (LinearLayout) view.findViewById(R.id.button1);
        codeET = ViewTool.createEditItem(inflater, "自定义代码", root1, false, false);
        codeET.setHint("请输入自定义代码");
        codeET.setText(getArguments().getString("MaterialNumber"));
        codeET.setEnabled(false);
        nameET = ViewTool.createEditItem(inflater, "自定义名称", root1, false, false);
        nameET.setHint("请输入自定义名称");
        nameET.setText(getArguments().getString("MaterialName"));
        nameET.setEnabled(false);

        regCodeET = ViewTool.createEditItem(inflater, "注册证号", root1, true, false);
        regCodeET.setHint("请输入注册证号");

        batchCodeET = ViewTool.createEditItem(inflater, "产品批号", root1, true, false);
        batchCodeET.setHint("请输入产品批号");

        startET = ViewTool.createEditItem(inflater, "有效开始日期", root1, true, true);
        startET.setHint("请选择开始日期");
        startET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time = startET.getText().toString();
                if (TextUtils.isEmpty(time)) {
                    time = Utils.currentTime();
                }
                new TimeDialogBuilder(getContext()).showDate().setTime(time).setCallback(new TimeDialogBuilder.TimePickCallback() {
                    @Override
                    public void setTime(String time) {
                        if (time.compareTo(Utils.currentDate()) > 0) {
                            Utils.showToast(getContext(), "开始时间不能超过当前日期");
                            return;
                        }
                        if (!TextUtils.isEmpty(endET.getText().toString()) && time.compareTo(endET.getText().toString()) > 0) {
                            Utils.showToast(getContext(), "开始时间不能大于结束时间!");
                            return;
                        }
                        startET.setText(time);
                    }
                }).create().show();
            }
        });

        endET = ViewTool.createEditItem(inflater, "有效结束日期", root1, true, true);
        endET.setHint("请选择结束日期");
        endET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time = endET.getText().toString();
                if (TextUtils.isEmpty(time)) {
                    time = Utils.currentTime();
                }
                new TimeDialogBuilder(getContext()).showDate().setTime(time).setCallback(new TimeDialogBuilder.TimePickCallback() {
                    @Override
                    public void setTime(String time) {
                        if (!TextUtils.isEmpty(startET.getText().toString()) && time.compareTo(startET.getText().toString()) < 0) {
                            Utils.showToast(getContext(), "结束时间不能小于开始时间!");
                            return;
                        }
                        endET.setText(time);
                    }
                }).create().show();
            }
        });

        bakEt = ViewTool.createEditItemNoLine(inflater, "备注", root1, false, false);
        bakEt.setHint("请输入备注");

        view.findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                postClick();
            }
        });

        view.findViewById(R.id.button2).setVisibility(View.GONE);
        view.findViewById(R.id.button3).setVisibility(View.GONE);
        view.findViewById(R.id.button4).setVisibility(View.GONE);
        view.findViewById(R.id.button5).setVisibility(View.GONE);
        if (item != null) {
            regCodeET.setText(item.getRegistrationCode());
            batchCodeET.setText(item.getBatchNumber());
            startET.setText(item.getIssueDate());
            endET.setText(item.getExpirationDate());
            bakEt.setText(item.getRemark());
            bakEt.setSelection(bakEt.length());
        }
    }

    @Override
    public int layoutId() {
        return R.layout.five_layout;
    }

    private void postClick() {
        if (TextUtils.isEmpty(regCodeET.getText().toString().trim())) {
            Utils.showToast(getContext(), "注册证号不能为空!");
            return;
        }
        if (TextUtils.isEmpty(batchCodeET.getText().toString().trim())) {
            Utils.showToast(getContext(), "产品批号不能为空!");
            return;
        }
        if (TextUtils.isEmpty(startET.getText().toString().trim())) {
            Utils.showToast(getContext(), "有效开始日期不能为空!");
            return;
        }
        if (TextUtils.isEmpty(endET.getText().toString().trim())) {
            Utils.showToast(getContext(), "有效结束日期不能为空!");
            return;
        }
        showLoadImg();
        Map<String, String> param = new HashMap<>();
        if (item == null) {
            param.put("reqType", "");
            param.put("distProductId", getArguments().getString("id"));
            param.put("id", "");
        } else {
            param.put("reqType", "1");
            param.put("id", item.getId());
        }
        param.put("registrationCode", regCodeET.getText().toString().trim());
        param.put("batchNumber", batchCodeET.getText().toString().trim());
        param.put("issueDate", startET.getText().toString().trim());
        param.put("expirationDate", endET.getText().toString().trim());
        param.put("remark", bakEt.getText().toString().trim());
        getHttpClient().post().url(Constant.EDIT_DIST_PRODUCT_REG).params(Constant.makeParam(param)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    dismissLoadImg();
                    Type type = new TypeToken<BaseObjResponse<MockObj>>() {
                    }.getType();
                    BaseObjResponse<MockObj> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        Utils.showToast(getContext(), "提交成功！");
                        RefershEvent event = new RefershEvent();
                        event.mclass = RegNumberFragment.class;
                        EventBus.getDefault().post(event);
                        getActivity().finish();
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {

                }
            }
        });
    }

}
